const app = getApp()
import { index, history, clear } from '../../api/search'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    back: true,
    title: '搜索',
    loading: true,
    history: [],
    keyword: ''
  },
  onLoad (options) {
    this.setData({ loading: true })
    history().then(res => {
      this.setData({
        history: res.data
      })
    })
      .finally(() => {
        this.setData({ loading: false })
      })
  },
  onShow () { },
  onPullDownRefresh () { /* 监听用户下拉动作*/ },
  onReachBottom () { /* 页面上拉触底事件的处理函数*/ },

  // ===================method================//
  setKeyWord (event) {
    app.redirect('/pages/goods/index/index?keyword=' + event.currentTarget.dataset.keyword)
  },
  onSearch (event) {
    const keyword = event.detail
    index(keyword)
    app.redirect('/pages/goods/index/index?keyword=' + keyword)
  },
  onClear () {
    const that = this
    Dialog.confirm({
      message: '确定清空吗？'
    }).then(() => {
      clear().then(res => {
        that.setData({ history: [] })
      })
    }).catch(() => {})
  },
  onCancel () {
    wx.navigateBack({
      delta: 1,
    })
  }
})